package com.lzy.imagepickerdemo.zn;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.imagepicker.ImageDataSource;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageFolder;
import com.lzy.imagepicker.ui.ImageBaseActivity;
import com.lzy.imagepickerdemo.R;
import com.lzy.imagepickerdemo.imageloader.GlideImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends ImageBaseActivity implements ImageDataSource.OnImagesLoadedListener {

    private static final int REQUEST_PERMISSION_STORAGE = 10001;
    @Bind(R.id.tv_add)
    TextView tvAdd;
    @Bind(R.id.tv_serach)
    TextView tvSerach;
    @Bind(R.id.rev_photo_list)
    RecyclerView revPhotoList;
    @Bind(R.id.rev_type_photo_list)
    RecyclerView revTypePhotoList;
    @Bind(R.id.rev_personal_photo_list)
    RecyclerView revPersonalPhotoList;
    @Bind(R.id.im_photo)
    ImageView imPhoto;
    ImagePicker imagePicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                new ImageDataSource(this, null, this);
            } else {
                showToast("权限被禁止，无法选择本地图片");
            }
        }
    }

    @OnClick({R.id.tv_add, R.id.tv_serach,R.id.im_photo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add:

                break;
            case R.id.tv_serach:

                break;
            case R.id.im_photo:
                Intent intent = new Intent(MainActivity.this, LookSeeActivity.class);
                //ImagePicker.getInstance().setSelectedImages(images);
                startActivityForResult(intent, 100);
                break;
        }
    }

    @Override
    public void onImagesLoaded(List<ImageFolder> imageFolders) {
        Log.i("程序", "程序返回数据->>>>" + imageFolders.size());
        initViews(imageFolders);
    }

    private void initViews(List<ImageFolder> imageFolders) {
        if (imageFolders.size()>0){
            imagePicker.getImageLoader().displayImage(this, imageFolders.get(0).cover.path, imPhoto, imageFolders.get(0).cover.width, imageFolders.get(0).cover.height);
        }

    }
}
